<?php

namespace App\Api\Dao;

use App\Exceptions\DaoException;
use App\Helpers\ResponseEnum;
use App\Models\TongUserApplyStore;

class ApplyStoreDao extends BaseDao
{

    /**
     * 查询构造器
     * @param $data
     * @return \Illuminate\Database\Eloquent\Builder
     */
    function getBuildQuery($data)
    {
        // TODO: Implement getBuildQuery() method.
        $query = TongUserApplyStore::query();
        if(checkIsHasField($data,'id')){
            $query->where('id',$data['id']);
        }
        if(checkIsHasField($data,'u_id')){
            $query->where('u_id',$data['u_id']);
        }
        return $query;
    }

    /**
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|void
     */
    public function applyStore(array $params)
    {
        if(checkIsHasField($params,'id')){
            unset($params['id']);
        }
        $result = TongUserApplyStore::query()->create($params);
        if(!$result){
            throw new DaoException(ResponseEnum::HTTP_ERROR,'提交失败');
        }
        return $result;
    }

    /**
     * 获取申请的详情
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|void
     */
    public function applyStoreInfo(array $params)
    {
        return $this->getBuildQuery($params)->orderBy('created_at','desc')->first();
    }
}
